const app = getApp()
import {
  reqConsumerAlarmPage,
  reqGetNoticeListByType,
  reqGetUnReadNoticeCount,
  reqUpdateNoticeStatusByParam
} from '../../../request/api.js'
import {
  showMsg
} from '../../../utils/config.js'

Page({
  data: {
    navMarginTop: app.globalData.navMarginTop + 'px', //导航栏高度
    navBarHeight: app.globalData.navBarHeight + 'px', //头部标题高度
    navHeight: app.globalData.navHeight + 'px', //头部距离
    bottomHeight: app.globalData.bottomHeight + 'px', //iphone底部距离

    showFamilyPopup: false, //家庭选择弹出
    familyInfo: "",

    currentTab: 0,
    tabList: [{
      id: 0,
      name: '设备',
      icon: 'icon-smarttongzhiguanli',
      count: 0,
    }, {
      id: 1,
      name: '家庭',
      icon: 'icon-smartjiatingyonghu',
      count: 0,
    }, {
      id: 2,
      name: '通知',
      icon: 'icon-smarta-tongzhi3',
      count: 0,
    }, ],

  },
  onLoad(options) {

  },

  onShow() {
    this.getTabBar().init();
    this.getGetUnReadNoticeCount() 
    if (!this.data.alarmIsNextPage) return
  },
  async getGetUnReadNoticeCount(){
    const res = await reqGetUnReadNoticeCount({
      userId: app.globalData.userId,
    })
    if (res.code !== 20000) return showMsg(res.message)
    console.log(res,'222');
    let unReadCount1=0
    let unReadCount2=0
    let unReadCount3=0
    res.data.unReadNotice.forEach(item=>{
      if(item.noticeType==3){
        unReadCount1=item.unReadCount//设备
      }
      if(item.noticeType==2){
        unReadCount2=item.unReadCount//家庭
      }
      if(item.noticeType==1){
        unReadCount3=item.unReadCount//系统
      }
    })
    this.setData({
      'tabList[0].count':unReadCount1,
      'tabList[1].count':unReadCount2,
      'tabList[2].count':unReadCount3
    })
    if(this.data.currentTab==0 && this.data.tabList[0].count>0){
      this.getUpdateNoticeStatusByParam(3)
    }else if(this.data.currentTab==1 && this.data.tabList[1].count>0){
      this.getUpdateNoticeStatusByParam(2)
    }else if(this.data.currentTab==2 && this.data.tabList[2].count>0){
      this.getUpdateNoticeStatusByParam(1)
    }
  },
  async getGetNoticeListByType(){
    const res = await reqGetNoticeListByType(1,10,{
      userId: app.globalData.userId,
      noticeType:'3'
    })
    if (res.code !== 20000) return showMsg(res.message)
    console.log(res);
  },
  // 批量消息已读
  async getUpdateNoticeStatusByParam(noticeType){
    console.log(noticeType);
    const res = await reqUpdateNoticeStatusByParam({
      receiverId: app.globalData.userId,
      noticeType:noticeType
    })
    if (res.code !== 20000) return showMsg(res.message)
    console.log(res);
    if(noticeType==3){
      this.setData({
        'tabList[0].count':0,
      })
    }else  if(noticeType==2){
      this.setData({
        'tabList[1].count':0,
      })
    }else  if(noticeType==1){
      this.setData({
        'tabList[2].count':0,
      })
    }
    // this.getGetUnReadNoticeCount()
  },
  

  onFamily() {
    this.setData({
      showFamilyPopup: true
    })
  },
  // familyHandle(e) {
  //   this.setData({
  //     showFamilyPopup: false,
  //   })
  //   if (e.detail.action == 'confirm') {
  //     if (e.detail.familyInfo.id) {
  //       // if (e.detail.familyInfo.id === this.data.familyInfo.id) return//lc
  //       if (e.detail.familyInfo.id === (this.data.familyInfo && this.data.familyInfo.id)) return
  //       this.setData({
  //         familyInfo: e.detail.familyInfo,
  //         scrollIntoRoom: '',
  //         roomActive: 0,
  //       })
  //     } else {
  //       // 场景：删除最后一个家庭时
  //       this.setData({
  //         familyInfo: {},
  //         scrollIntoRoom: '',
  //         roomActive: 0,
  //         roomList: [],
  //         dataList: [],
  //       })
  //     }
  //   }
  // },

  swichTab(e) {
    let {
      id
    } = e.currentTarget.dataset
    this.setData({
      currentTab: id
    })
  },
  onSwiperChange(e) {
    const {
      current
    } = e.detail
    console.log(current, 'current');
    this.setData({
      currentTab: current
    })
    if(current==0 && this.data.tabList[0].count>0){
      this.getUpdateNoticeStatusByParam(3)
    }else if(current==1 && this.data.tabList[1].count>0){
      this.getUpdateNoticeStatusByParam(2)
    }else if(current==2 && this.data.tabList[2].count>0){
      this.getUpdateNoticeStatusByParam(1)
    }
  },
})